﻿Imports Tajan.Monitoring.Service
Imports SignalR

Public Class MonitoringConnectionIdGenerator
    Implements IConnectionIdGenerator

    Public Const CONNECTION_ID_COOKIE_NAME As String = "SIGNALR_CONNECTION_ID"

    Public Function GenerateConnectionId(request As IRequest) As String Implements IConnectionIdGenerator.GenerateConnectionId

        If request.Cookies(CONNECTION_ID_COOKIE_NAME) Is Nothing Then

            'create new connection and get the connection id
            Dim newConnection As Entities.Connection = (New Manager).CreateNewConnection(HttpContext.Current.Request)

            'save the connection id in client's cookie
            HttpContext.Current.Response.Cookies.Add(New HttpCookie(CONNECTION_ID_COOKIE_NAME, newConnection.ConnectionId))

        End If

        Return request.Cookies(CONNECTION_ID_COOKIE_NAME).Value

    End Function

End Class
